﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RelatorioDinamico.WebUI.Models;

namespace RelatorioDinamico.WebUI.Controllers
{
    public class RelatorioDinamicoController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [ValidateInput(false)]
        public ActionResult Criar(string arvore)
        {

            return View();
        }

        public ActionResult TabelasDinamicas(string tabelaBase = null)
        {
            PreencherViewBagsTabelas(tabelaBase);

            return PartialView();
        }

        public ActionResult ColunasDinamicas(string tabela)
        {
            var model = new Domain.Util.RelatorioDinamico().SelecionarColunasPrimitivas(tabela);

            return PartialView(model);
        }

        public ActionResult Referencias(string tabela)
        {
            var models = new Domain.Util.RelatorioDinamico().SelecionarColunasReferencias(tabela)
                .Select(r => new ReferenciaModel { Tabela = r.Value, TabelaNamespace = r.Key });

            PreencherViewBagsReferencias();

            return PartialView(models);
        }

        private void PreencherViewBagsTabelas(string tabelaBase = null)
        {
            var tabelas = new Domain.Util.RelatorioDinamico().SelecionarTabelasDinamicas(tabelaBase);
            ViewData["Tabelas"] = tabelas.Select(t => new SelectListItem { Text = t.Value, Value = t.Key });
        }

        private void PreencherViewBagsReferencias()
        {
            ViewData["TipoReferencia"] = WebExtension.ToSelectList(typeof(TipoReferencia), true, true, null);
        }
    }
}
